package com.isay.frameworklib.ui;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.gyf.barlibrary.ImmersionBar;
import com.isay.frameworklib.R;
import com.isay.frameworklib.utils.ToastUtils;
import com.isay.frameworklib.widget.loading.LoadingDialog;

/**
 * Created by cywu4 on 2018/8/6.
 */

public abstract class BaseActivity extends AppCompatActivity {

    private LoadingDialog mLoadingDialog = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        onCreateActivity(savedInstanceState);

        if (getStatusBarColor() == 0) {
            // 所有子类都将继承这些相同的属性,请在设置界面之后设置
            ImmersionBar.with(this).init();
        } else {
            //使用该属性,必须指定状态栏颜色
            ImmersionBar.with(this)
                    .fitsSystemWindows(true)
                    .statusBarColor(getStatusBarColor())
                    .statusBarDarkFont(true, 0.2f)
                    .init();
        }
    }

    protected abstract int getStatusBarColor();

    protected abstract void onCreateActivity(Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.with(this).destroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 如果你的app可以横竖屏切换，并且适配4.4或者emui3手机请务必在onConfigurationChanged方法里添加这句话
        ImmersionBar.with(this).init();
    }

    protected void showLoading(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.showDialog(msg);
        }
    }

    protected void dismissDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismissDialog();
        }
    }

    @Override
    public void finish() {
        super.finish();
        ToastUtils.cancel();
    }

}
